package com.woniuxy.microsysserver.controller;

import com.woniuxy.common.bean.ResultMsg;
import com.woniuxy.microsysserver.bean.AdminInfo;
import com.woniuxy.microsysserver.bean.RoleInfo;
import com.woniuxy.microsysserver.service.IRoleService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("role")
public class RoleController {

    @Resource
    private IRoleService roleServiceImpl;

    @GetMapping("/all")
    ResultMsg findAll(){
        List<RoleInfo> all = roleServiceImpl.findAll();
        ResultMsg resultMsg = new ResultMsg(true);
        resultMsg.setData(all);
        return resultMsg;
    }

    @PutMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    ResultMsg update(@RequestBody RoleInfo roleInfo){
        System.out.println(roleInfo);
        roleServiceImpl.update(roleInfo);
        return new ResultMsg(true);
    }

    @PutMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
    ResultMsg delete(Integer id){
        roleServiceImpl.delete(id);
        return new ResultMsg(true);
    }

    @PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_VALUE)
    ResultMsg save(@RequestBody RoleInfo roleInfo){
        roleServiceImpl.add(roleInfo);
        return new ResultMsg(true);
    }

    @GetMapping("/findByRoleId")
    ResultMsg findByRoleId( Integer id){
        List<AdminInfo> datas = roleServiceImpl.findByRoleId(id);
        ResultMsg resultMsg = new ResultMsg(true);
        resultMsg.setData(datas);
        return resultMsg;
    }

    @PutMapping(value = "/changePermit",produces = MediaType.APPLICATION_JSON_VALUE)
    ResultMsg changePermit(@RequestBody RoleInfo roleInfo){
//        System.out.println(roleInfo.getPermit());
        roleServiceImpl.changePermit(roleInfo.getId(), roleInfo.getPermit());
        return new ResultMsg(true);
    }

    @GetMapping("/findById")
    ResultMsg findById(Integer id){
        RoleInfo role = roleServiceImpl.findById(id);
        ResultMsg resultMsg = new ResultMsg(true);
        resultMsg.setData(role);
        return resultMsg;
    }
}
